//
//  LeftViewController.m
//  PlayerFix
//
//  Created by dllo on 15/11/3.
//  Copyright © 2015年 DAT. All rights reserved.
//

#import "LeftViewController.h"
#import "WeaterView.h"
#import "SearchResultTableViewCell.h"
#import "AboutUsViewController.h"
#import "MyCollectionViewController.h"
#import "SendMailViewController.h"
@interface LeftViewController ()<UISearchBarDelegate,UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)UISearchBar *searchBar;
@property(nonatomic,strong)NSMutableArray *resultArray;
@property (nonatomic, strong) UILabel *aboutUs;
@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UILabel *myCollection;
@property (nonatomic, strong) UILabel *clearLabel;
@property (nonatomic, strong) UILabel *connectUs;

@end

@implementation LeftViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    
//    self.resultArray = [NSMutableArray array];
//    self.view.backgroundColor = [UIColor orangeColor];
//    UIImageView *backImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"F_{}[R1L5V%M(F9Q_R7BDDQ.jpg"]];
//    backImageView.frame = self.view.frame;
//    [self.view addSubview:backImageView];
//    backImageView.userInteractionEnabled = YES;
    self.view.backgroundColor = [UIColor colorWithRed:30 / 255.0 green:80 / 255.0 blue:100 / 255.0 alpha:1];
     WeaterView *weaterView = [[WeaterView alloc]initWithFrame:CGRectMake(0, 50, 270, 150)];
//    weatherView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:weaterView];
    
    self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(10, 20, 330, 25)];
    _searchBar.placeholder = @"Search";
    [self.view addSubview:_searchBar];
    _searchBar.showsCancelButton = YES;
    _searchBar.delegate = self;
    _searchBar.barTintColor = [UIColor whiteColor];
    _searchBar.tintColor = [UIColor whiteColor];
    _searchBar.searchBarStyle = 2;
    //更改searchBar.text的文字颜色
    for (UIView *subView in _searchBar.subviews)
    {
        for (UIView *secondLevelSubview in subView.subviews){
            if ([secondLevelSubview isKindOfClass:[UITextField class]])
            {
                UITextField *searchBarTextField = (UITextField *)secondLevelSubview;
                //set font color here
                searchBarTextField.textColor = [UIColor whiteColor];
                break;
            }
        }
    }
    
    self.bgView = [[UIView alloc] initWithFrame:CGRectMake(5, 200 , 300, 270)];
    self.bgView.backgroundColor = [UIColor whiteColor];
    self.bgView.alpha = 0.3;
    [self.view addSubview:self.bgView];
    
    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(5, 266, 300, 2)];
    label1.backgroundColor = [UIColor whiteColor];
    label1.alpha = 0.8;
    [self.view addSubview:label1];
    
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(5, 332, 300, 2)];
    label2.backgroundColor = [UIColor whiteColor];
    label2.alpha = 0.8;
    [self.view addSubview:label2];
    
    UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(5, 398, 300, 2)];
    label3.backgroundColor = [UIColor whiteColor];
    label3.alpha = 0.8;
    [self.view addSubview:label3];
    
    self.myCollection = [[UILabel alloc] initWithFrame:CGRectMake(5, 205, 300, 64)];
    self.myCollection.textColor = [UIColor whiteColor];
    self.myCollection.text = @"    我的收藏";
    self.myCollection.font = [UIFont systemFontOfSize:19];
    self.myCollection.userInteractionEnabled = YES;
    UITapGestureRecognizer *collectionTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushToMyCollection:)];
    [self.myCollection addGestureRecognizer:collectionTap];
    [self.view addSubview:self.myCollection];
    
    self.clearLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 273, 300, 64)];
    self.clearLabel.textColor = [UIColor whiteColor];
    self.clearLabel.text = @"    清除缓存";
    self.clearLabel.userInteractionEnabled = YES;
    self.clearLabel.font = [UIFont systemFontOfSize:19];
    UITapGestureRecognizer *clearTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clearAction:)];
    [self.clearLabel addGestureRecognizer:clearTap];
    [self.view addSubview:self.clearLabel];
    
    self.connectUs = [[UILabel alloc] initWithFrame:CGRectMake(5, 405, 300, 64)];
    self.connectUs.textColor = [UIColor whiteColor];
    self.connectUs.text = @"    意见反馈";
    self.connectUs.font = [UIFont systemFontOfSize:19];
    self.connectUs.userInteractionEnabled = YES;
    UITapGestureRecognizer *connectTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushToSendMail:)];
    [self.connectUs addGestureRecognizer:connectTap];
    [self.view addSubview:self.connectUs];
    
    self.aboutUs = [[UILabel alloc] initWithFrame:CGRectMake(5, 339, 300, 64)];
    self.aboutUs.textColor = [UIColor whiteColor];
    self.aboutUs.text = @"    关于我们";
    self.aboutUs.font = [UIFont systemFontOfSize:19];
    self.aboutUs.userInteractionEnabled = YES;
    UITapGestureRecognizer *aboutUsTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushToAboutUs:)];
    [self.aboutUs addGestureRecognizer:aboutUsTap];
    [self.view addSubview:self.aboutUs];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, - 667, 375, 667)];
    ITRAirSideMenu *itrSideMenu = ((AppDelegate *)[UIApplication sharedApplication].delegate).itrAirSideMenu;
    [itrSideMenu.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor colorWithRed:30 / 255.0 green:100 / 255.0 blue:120 / 255.0 alpha:1];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
}


#pragma -- 我的收藏
- (void)pushToMyCollection:(UITapGestureRecognizer *)tap{
    
    MyCollectionViewController *myCollectionVC = [[MyCollectionViewController alloc] init];
    myCollectionVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:myCollectionVC animated:YES completion:^{
        
        
    }];
}


#pragma -- 清除缓存
- (void)clearAction:(UITapGestureRecognizer *)tap{
    
    NSString *title = NSLocalizedString(@"提示", nil);
    NSString *message = NSLocalizedString(@"是否清除缓存?", nil);
    NSString *cancelButtonTitle = NSLocalizedString(@"取消", nil);
    NSString *allowedButtonTitle = NSLocalizedString(@"确定", nil);
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    // Create the actions.
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        NSLog(@"The \"Okay/Cancel\" alert's cancel action occured.");
    }];
    
    UIAlertAction *allowedAction = [UIAlertAction actionWithTitle:allowedButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSLog(@"The \"Okay/Cancel\" alert's other action occured.");
        
        [[SDImageCache sharedImageCache] clearDisk];
        //[self.bgTableView reloadData];
    }];
    
    // Add the actions.
    [alertController addAction:cancelAction];
    [alertController addAction:allowedAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
}

#pragma -- 关于我们

- (void)pushToAboutUs:(UITapGestureRecognizer *)tap{
    
    AboutUsViewController *aboutUsVC = [[AboutUsViewController alloc] init];
    aboutUsVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    //[[SingletonForPush shareSingleton].naVC pushViewController:aboutUsVC animated:YES];
    [self presentViewController:aboutUsVC animated:YES completion:^{
        
        
    }];
}

#pragma -- 意见反馈
-  (void)pushToSendMail:(UITapGestureRecognizer *)tap{
    
    SendMailViewController *sendMailVC = [[SendMailViewController alloc] init];
    sendMailVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:sendMailVC animated:YES completion:^{
        
        
    }];
}

-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    [UIView animateWithDuration:0.3 animations:^{
        
        _tableView.frame = CGRectMake(0, - 667, 375, 667);
        [self.view endEditing:YES];
        
    }];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    NSLog(@"走了2");
    [UIView animateWithDuration:0.3 animations:^{
        
        _tableView.frame = CGRectMake(0, 50, 375, 667 - 50);
        
    }];
    
}

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    self.resultArray = [NSMutableArray array];

    [self getAFNetWorkingData];
    
    
    [self.view endEditing:YES];
    
    
}

-(void)getAFNetWorkingData
{
    NSString *string = [NSString stringWithFormat:@"http://echosystem.kibey.com/sound/search?keyword=%@&page=1",_searchBar.text];
    NSString *encodingString = [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [DATAsyncGetAFNBlock GetAFNURLWithString:encodingString andBody:nil andCookie:@"PHPSESSID=8gg17npqfr77fmjf9uol3is636" resultBlock:^(id result) {
    
//    NSLog(@"result: %@", result);
        
    NSDictionary *dic = result;
    NSDictionary *resultDic = [dic objectForKey:@"result"];
    NSArray *array = [resultDic objectForKey:@"data"];
    for (NSDictionary *dic in array) {
        SearchResultModel *model = [SearchResultModel new];
        [model setValuesForKeysWithDictionary:dic];
        [self.resultArray addObject:model];
    }
        if (_resultArray.count != 0) {

            [self.tableView reloadData];
            _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;

        }else {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Sorry" message:@"未找到搜索结果" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                
                [UIView animateWithDuration:0.3 animations:^{
                    
                    _tableView.frame = CGRectMake(0, - 667, 375, 667);
                    
                }];
                
            }];
            [alert addAction:action];
            [self presentViewController:alert animated:YES completion:^{
                
                
            }];
            
        }
    
    } errorBlock:^(NSError *error) {
    
    
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_resultArray.count > 0) {
        
        return _resultArray.count;
    }else {
        return 0;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SearchResultTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[SearchResultTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    if (_resultArray.count != 0) {
        
        SearchResultModel *model = _resultArray[indexPath.row];
        cell.textLabel.text = model.name;
    }
    
    return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
